home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CTOOLS10.ARJ / LISTTST.C < prev    next >
C/C++ Source or Header  |  1991-12-31  |  3KB  |  138 lines

  1. /* Simple program to test the list routines */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <dos.h>
  8. #include <alloc.h>
  9. #include "list.h"
  10.  
  11. #ifdef    TEST1
  12.  
  13. typedef struct {
  14.     char    name[40];
  15.     int        age;
  16.     } REC;
  17.  
  18. int my_cmp(REC *r1,REC *r2)
  19. {
  20.     return strcmp(r1->name,r2->name);
  21. }
  22.  
  23. void main(void)
  24. {
  25.     LIST        *list;
  26.     int            done = 0;
  27.     REC            *rec;
  28.     char        line[80];
  29.  
  30.     list = lst_init();
  31.  
  32.     printf("Type a list of names and ages. Empty line quits\n\n");
  33.  
  34.     while (!done) {
  35.         rec = lst_newnode(sizeof(REC));
  36.         gets(line);
  37.         if ((done = (line[0] == '\0')) != 1) {
  38.             strcpy(rec->name,line);
  39.             gets(line);
  40.             rec->age = atoi(line);
  41.             lst_insertafter(list,rec,LST_HEAD(list));
  42.             }
  43.         };
  44.  
  45.     printf("\nThe list you typed in was:\n\n");
  46.  
  47.     for (rec = lst_first(list); rec; rec = lst_next(rec))
  48.         printf("Name: %s, Age: %d\n",rec->name,rec->age);
  49.  
  50.     printf("\nSorting the list...\n\n");
  51.  
  52.     lst_mergesort(list,my_cmp);
  53.  
  54.     for (rec = lst_first(list); rec; rec = lst_next(rec))
  55.         printf("Name: %s, Age: %d\n",rec->name,rec->age);
  56.  
  57.     lst_kill(list,lst_freenode);
  58. }
  59. #endif
  60.  
  61. #ifdef    TEST2
  62.  
  63. /* A implementation of a macro to peek at a long value (not in DOS.H)    */
  64.  
  65. #define    peekl(__segment,__offset) (*((long  far*)MK_FP(__segment, __offset)))
  66.  
  67. void randstr(char *s,int length)
  68. {
  69.     int    i;
  70.  
  71.     for (i = 0; i < length; i++)
  72.         s[i] = 'a' + (char)random('z' - 'a');
  73.     s[i] = '\0';
  74. }
  75.  
  76. char *timestr(long time)
  77. {
  78.     int            minutes,seconds,sec100;
  79.     float        totalsecs;
  80.     static char    str[10];
  81.  
  82.     totalsecs = time / 18.2;
  83.     minutes = (int)totalsecs / 60;
  84.     totalsecs -= minutes * 60;
  85.     seconds = (int)totalsecs;
  86.     sec100 = (int)((totalsecs - seconds) * 100);
  87.     sprintf(str,"%02d:%02d.%02d",minutes,seconds,sec100);
  88.     return str;
  89. }
  90.  
  91. void main(void)
  92. {
  93.     LIST        *list;
  94.     char        *s;
  95.     char        line[80];
  96.     int            size,length;
  97.     long        start,finish;
  98.  
  99.     printf("Size of list to create: ");
  100.     gets(line);
  101.     size = atoi(line);
  102.     printf("Maximum length of strings: ");
  103.     gets(line);
  104.     length = atoi(line);
  105.  
  106.     printf("\nMemory at start: %ld\n",coreleft());
  107.     printf("\nCreating list of %d random strings of length %d ...\n\n",size,length);
  108.  
  109.     randomize();
  110.     list = lst_init();
  111.  
  112.     for (; size; size--) {
  113.         s = lst_newnode(length+1);
  114.         randstr(s,random(length));
  115.         lst_insertafter(list,s,LST_HEAD(list));
  116.         }
  117.  
  118.     printf("Sorting strings...\n\n");
  119.  
  120.     start = peekl(0x40,0x6C);
  121.     lst_mergesort(list,strcmp);
  122.     finish = peekl(0x40,0x6C);
  123.  
  124.     printf("Time to sort: %s\n",timestr(finish-start));
  125.  
  126. #ifdef    PRINT_LIST
  127.     for (s = lst_first(list); s; s = lst_next(s))
  128.         printf("String: %s\n",s);
  129. #endif
  130.  
  131.     printf("\nMemory before deleting list: %ld\n",coreleft());
  132.  
  133.     lst_kill(list,lst_freenode);
  134.  
  135.     printf("\nMemory after deleting list: %ld\n",coreleft());
  136. }
  137.  
  138. #endif